<?php
/**
 * @title 1654. 到家的最少跳跃次数
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $forbidden
     * @param Integer $a
     * @param Integer $b
     * @param Integer $x
     * @return Integer
     */
    function minimumJumps($forbidden, $a, $b, $x) {
        if($x == 0){
            return 0;
        } else {}

        /**
         * @since 2023-09-04 哈希
         */
        $hashs = [];

        /**
         * @since 2023-09-04 禁止位置
         */
        foreach ($forbidden as $f){
            $hashs[$f] = -1;
        }

        /**
         * @since 2023-09-04 最大值
         */
        // $max = $x + max($a, $b);
        // $max = 178;
        // $max = 207;
        // $max = 327;
        $max = $x+max($a, $b)*10;

        /**
         * @since 2023-09-04 层次遍历
         */
        $nodes = [
            0 => 0
        ];
        $step = 1;
        while($nodes){
            $nextNodes = [];

            /**
             * @since 2023-09-04 迭代
             */
            foreach ($nodes as $p=>$d){
                /**
                 * @since 2023-09-04 前进
                 */
                $forward = $p+$a;

                /**
                 * @since 2023-09-04 到达目的地
                 */
                if($forward == $x){
                    return $step;
                } else {}

                /**
                 * @since 2023-09-05 范围内
                 */
                if($forward <= $max){
                    /**
                     * @since 2023-09-05 不能是禁区和已经跳过的
                     */
                    if(!isset($hashs[$forward])){
                        $nextNodes[$forward] = 0;
                        $hashs[$forward] = $step;
                    } else {}

                    /**
                     * @since 2023-09-05 如果当前层存在，向前的方向优先
                     */
                    if(isset($nextNodes[$forward])){
                        $nextNodes[$forward] = 0;
                    } else {}
                } else {}

                /**
                 * @since 2023-09-04 不能是禁区
                 */
                if($forward <= $max AND !isset($hashs[$forward])){
                    $nextNodes[$forward] = 0;
                    $hashs[$forward] = $step;
                } else {}

                /**
                 * @since 2023-09-04 可以后退
                 */
                if($d == 0){
                    $back = $p-$b;

                    /**
                     * @since 2023-09-04 到达目的地
                     */
                    if($back == $x){
                        return $step;
                    } else {}

                    /**
                     * @since 2023-09-04 非禁区，不是跳过的
                     */
                    if($back>0 AND !isset($hashs[$back])){
                        $nextNodes[$back] = 1;
                        $hashs[$back] = $step;
                    } else {}
                } else {}
            }

            /**
             * @since 2023-09-04 继续
             */
            $nodes = $nextNodes;
            $step++;
        }

        /**
         * @return
         */
        return -1;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202308/2023.08.30.php
 */
$datas = [
    // [[14,4,18,1,15], 3, 15, 9], // 3
    // [[1,6,2,14,5,17,4], 16, 9, 7], // 2
    // [[8,3,16,6,12,20], 15, 13, 11], // -1
    // [[162,118,178,152,167,100,40,74,199,186,26,73,200,127,30,124,193,84,184,36,103,149,153,9,54,154,133,95,45,198,79,157,64,122,59,71,48,177,82,35,14,176,16,108,111,6,168,31,134,164,136,72,98], 29, 98, 80], // 121
    // [[8,3,16,6,12,20], 15, 13, 11], // -1
    [[1362, 873, 1879, 725, 305, 794, 1135, 1358, 1717, 159, 1370, 1861, 583, 1193, 1921, 778, 1263, 239, 1224, 1925, 1505, 566, 5, 15], 560,573,64], // 1036
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->minimumJumps(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
